<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

/**
 * SrcResponseSet
 *
 * Responses by a Source to an Inquiry
 *
 * @property integer $srs_id
 * @property integer $srs_src_id
 * @property integer $srs_inq_id
 * @property timestamp $srs_date
 * @property string $srs_uri
 * @property string $srs_type
 * @property boolean $srs_public_flag
 * @property boolean $srs_delete_flag
 * @property boolean $srs_translated_flag
 * @property boolean $srs_export_flag
 * @property integer $srs_loc_id
 * @property string $srs_conf_level
 * @property integer $srs_cre_user
 * @property integer $srs_upd_user
 * @property timestamp $srs_cre_dtim
 * @property timestamp $srs_upd_dtim
 * @property Source $Source
 * @property Inquiry $Inquiry
 * @property Locale $Locale
 * @property Doctrine_Collection $SrcResponse
 * @property Doctrine_Collection $SrsAnnotation
 * @author rcavis
 * @package default
 */
class SrcResponseSet extends AIR2_Record {
    /* code_master values */
    public static $TYPE_INQUIRY = 'I';
    public static $TYPE_FEED = 'F';
    public static $TYPE_MAIL_MERGE = 'M';
    public static $TYPE_SUGGESTION = 'S';
    public static $CONF_HIGH = 'H';
    public static $CONF_LOW = 'L';
    public static $CONF_NOT = 'N';


    /**
     * Set the table columns
     */
    public function setTableDefinition() {
        $this->setTableName('src_response_set');
        $this->hasColumn('srs_id', 'integer', 4, array(
                'primary' => true,
                'autoincrement' => true,
            ));
        $this->hasColumn('srs_src_id', 'integer', 4, array(
                'notnull' => true,
            ));
        $this->hasColumn('srs_inq_id', 'integer', 4, array(
                'notnull' => true,
            ));
        $this->hasColumn('srs_date', 'timestamp', null, array(
                'notnull' => true,
            ));
        $this->hasColumn('srs_uri', 'string', 255, array(

            ));
        $this->hasColumn('srs_type', 'string', 1, array(
                'fixed' => true,
                'notnull' => true,
                'default' => self::$TYPE_INQUIRY,
            ));
        $this->hasColumn('srs_public_flag', 'boolean', null, array(
                'notnull' => true,
                'default' => false,
            ));
        $this->hasColumn('srs_delete_flag', 'boolean', null, array(
                'notnull' => true,
                'default' => false,
            ));
        $this->hasColumn('srs_translated_flag', 'boolean', null, array(
                'notnull' => true,
                'default' => false,
            ));
        $this->hasColumn('srs_export_flag', 'boolean', null, array(
                'notnull' => true,
                'default' => false,
            ));
        $this->hasColumn('srs_loc_id', 'integer', 4, array(
                'default' => 52, /* en_US -- see Locale.yml */
                'notnull' => true,
            ));
        $this->hasColumn('srs_conf_level', 'string', 1, array(
                'fixed' => true,
            ));
        $this->hasColumn('srs_cre_user', 'integer', 4, array(
                'notnull' => true,
            ));
        $this->hasColumn('srs_upd_user', 'integer', 4, array(

            ));
        $this->hasColumn('srs_cre_dtim', 'timestamp', null, array(
                'notnull' => true,
            ));
        $this->hasColumn('srs_upd_dtim', 'timestamp', null, array(

            ));

        parent::setTableDefinition();
    }



    /**
     * Set table relations
     */
    public function setUp() {
        parent::setUp();
        $this->hasOne('Source', array(
                'local' => 'srs_src_id',
                'foreign' => 'src_id',
                'onDelete' => 'CASCADE',
            ));
        $this->hasOne('Inquiry', array(
                'local' => 'srs_inq_id',
                'foreign' => 'inq_id',
                'onDelete' => 'CASCADE',
            ));
        $this->hasMany('SrcResponse', array(
                'local' => 'srs_id',
                'foreign' => 'sr_srs_id'
            ));
        $this->hasMany('SrsAnnotation', array(
                'local' => 'srs_id',
                'foreign' => 'srsan_srs_id'
            ));
        $this->hasOne('Locale', array(
                'local' => 'srs_loc_id',
                'foreign' => 'loc_id'
            ));

        // Tagging
        $this->hasMany('TagResponseSet as Tags', array(
                'local' => 'srs_id',
                'foreign' => 'tag_xid'
            ));
    }


    /**
     *
     *
     * @param User    $user
     * @return authz integer
     */
    public function user_may_read($user) {
        if ($user->is_system()) {
            return AIR2_AUTHZ_IS_SYSTEM;
        }       

        $inq_authz = $this->Inquiry->user_may_read($user);
        $src_authz = $this->Source->user_may_read($user);
        if ($inq_authz && $src_authz) {
            return $inq_authz;  // TODO more specific?
        }
        return AIR2_AUTHZ_IS_DENIED;
    }


    /**
     *
     *
     * @param User    $user
     * @return authz integer
     */
    public function user_may_write($user) {
        if ($user->is_system()) {
            return AIR2_AUTHZ_IS_SYSTEM;
        }       

        $inq_authz = $this->Inquiry->user_may_write($user);
        $src_authz = $this->Source->user_may_write($user);
        if ($inq_authz && $src_authz) {
            return $inq_authz;  // TODO more specific?
        }
        return AIR2_AUTHZ_IS_DENIED;
    }


    /**
     *
     *
     * @param User    $user
     * @return authz integer
     */
    public function user_may_manage($user) {
        if ($user->is_system()) {
            return AIR2_AUTHZ_IS_SYSTEM;
        }       

        $inq_authz = $this->Inquiry->user_may_manage($user);
        $src_authz = $this->Source->user_may_manage($user);
        if ($inq_authz && $src_authz) {
            return $inq_authz;  // TODO more specific?
        }
        return AIR2_AUTHZ_IS_DENIED;
    }


    /**
     * Apply authz rules for who may view the existence of a SrcResponseSet.
     *
     * @param AIR2_Query $q     (reference)
     * @param User    $u     (reference)
     * @param string  $alias (optional)
     */
    public static function query_may_read(AIR2_Query &$q, User &$u, $alias=null) {
        if ($u->is_system()) {
            return;
        }
        //Carper::carp("SRS::query_may_read: " . $q->getDql() . " alias=$alias");
        $a = ($alias) ? "$alias." : "";

        // the intersection of Source and Inquiry
        $srcq = AIR2_Query::create();
        $srcq->select('src.src_id');
        $srcq->from('Source src');
        Source::query_may_read($srcq, $u);

        $inqq = AIR2_Query::create();
        $inqq->select('inq.inq_id');
        $inqq->from('Inquiry inq');
        Inquiry::query_may_read($inqq, $u);

        $subq = "${a}srs_src_id IN (" . $srcq->getDql() . ") AND ${a}srs_inq_id IN (" . $inqq->getDql() . ")";
        //Carper::carp("SRS subq: $subq");
        $q->addWhere($subq);
        //Carper::carp($q->getDql());
        //Carper::carp($q->getSqlQuery());
    }


    /**
     * Apply authz rules for who may write to a SrcResponseSet.
     *
     * @param AIR2_Query $q     (reference)
     * @param User    $u     (reference)
     * @param string  $alias (optional)
     */
    public static function query_may_write(AIR2_Query &$q, User &$u, $alias=null) {
        if ($u->is_system()) {
            return;
        }
        $a = ($alias) ? "$alias." : "";

        // the intersection of Source and Inquiry
        $srcq = $q->createSubquery();
        $srcq->select('src.src_id');
        $srcq->from('Source src');
        Source::query_may_write($srcq, $u);

        $inqq = $q->createSubquery();
        $inqq->select('inq.inq_id');
        $inqq->from('Inquiry inq');
        Inquiry::query_may_write($inqq, $u);

        $q->addWhere("${a}srs_src_id IN (" . $srcq->getDql() . ")");
        $q->addWhere("${a}srs_inq_id IN (" . $inqq->getDql() . ")");
        //diag($q->getSqlQuery());

    }


    /**
     * Apply authz rules for who may manage a SrcResponseSet.
     *
     * @param AIR2_Query $q     (reference)
     * @param User    $u     (reference)
     * @param string  $alias (optional)
     */
    public static function query_may_manage(AIR2_Query &$q, User &$u, $alias=null) {
        if ($u->is_system()) {
            return;
        }
        $a = ($alias) ? "$alias." : "";

        // the intersection of Source and Inquiry
        $srcq = $q->createSubquery();
        $srcq->select('src.src_id');
        $srcq->from('Source src');
        Source::query_may_manage($srcq, $u);

        $inqq = $q->createSubquery();
        $inqq->select('inq.inq_id');
        $inqq->from('Inquiry inq');
        Inquiry::query_may_manage($inqq, $u);

        $q->addWhere("${a}srs_src_id IN (" . $srcq->getDql() . ")");
        $q->addWhere("${a}srs_inq_id IN (" . $inqq->getDql() . ")");
        //diag($q->getSqlQuery());

    }


}
